Skip to content

Document EF Core 9 breaking change: NoTrackingWithIdentityResolution restricted for JSON collection queries#5413

Open
AndriySvyryd with Copilot wants to merge 5 commits into
mainfrom
copilot/document-breaking-change-notrackidentityresol
Open

Document EF Core 9 breaking change: NoTrackingWithIdentityResolution restricted for JSON collection queries#5413
AndriySvyryd with Copilot wants to merge 5 commits into
mainfrom
copilot/document-breaking-change-notrackidentityresol

Conversation

Copilot AI commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #4932

Using AsNoTrackingWithIdentityResolution() with JSON-mapped entity collections could silently corrupt data or throw unhelpful exceptions in EF Core 8 and earlier, due to how JSON is streamed and materialized. EF Core 9 adds a validator and explicit prohibitions to surface these problems clearly.

Changes

  • Summary table: Added Low-impact entry for the new restriction in ef-core-9.0/breaking-changes.md
  • Breaking change section: Documents the full behavior change — old (silent corruption / cryptic StartObject exception), new (explicit exception), why (key values not propagated for JSON collection queryable operators → null keys), and mitigations

Prohibited pattern (now throws)

var blogs = await context.Blogs
    .AsNoTrackingWithIdentityResolution()
    .Select(b => new
    {
        Blog = b,
        TopPosts = b.JsonPosts.OrderBy(p => p.Rating).Take(3).ToList() // ❌ queryable operator on JSON collection
    })
    .ToListAsync();

Mitigations

Switch to AsTracking() (for identity resolution) or AsNoTracking() (no deduplication needed).

Copilot AI changed the title [WIP] Add validator for entity materialization order Document EF Core 9 breaking change: NoTrackingWithIdentityResolution restricted for JSON collection queries Jul 3, 2026
Copilot AI requested a review from AndriySvyryd July 3, 2026 02:00
@AndriySvyryd AndriySvyryd requested a review from Copilot July 3, 2026 02:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Documents an EF Core 9 breaking change that prohibits certain AsNoTrackingWithIdentityResolution() query patterns involving JSON-mapped entity collections, to prevent incorrect materialization and to surface clearer exceptions.

Changes:

  • Added a new Low-impact entry to the EF Core 9 breaking changes summary table.
  • Added a new breaking change section describing old vs. new behavior, rationale, and mitigations (switch to AsTracking() or AsNoTracking()).

Comment thread entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md Outdated
Comment thread entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md Outdated
Comment thread entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 3, 2026 04:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.


#### Old behavior

Previously, using <xref:Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsNoTrackingWithIdentityResolution*> (or setting <xref:Microsoft.EntityFrameworkCore.QueryTrackingBehavior.NoTrackingWithIdentityResolution>) with queries that include JSON-mapped entity collections could silently produce incorrect results or data corruption, depending on the order in which entities were processed during materialization. Additionally, such queries could throw an unhelpful `Invalid token type: 'StartObject'` exception in some scenarios.

#### New behavior

Starting with EF Core 9.0, EF Core restricts the use of <xref:Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsNoTrackingWithIdentityResolution*> for certain JSON collection query patterns, to prevent silent data corruption:

Starting with EF Core 9.0, EF Core restricts the use of <xref:Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsNoTrackingWithIdentityResolution*> for certain JSON collection query patterns, to prevent silent data corruption:

- If entity instances in a JSON collection would be materialized in an order that could cause data corruption, EF Core throws an exception instructing the user to use a different tracking behavior.

#### Why

The combination of <xref:Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsNoTrackingWithIdentityResolution*> and JSON collections could silently produce incorrect materialized objects due to how JSON is streamed from the database: nested includes in JSON are part of the parent's materialization rather than being materialized separately. The stand-alone change tracker used for identity resolution relies on key values to deduplicate entity instances, but when LINQ operators are applied to JSON collections, EF Core cannot reliably propagate those key values to the materializer, resulting in entities with null keys and potential data corruption.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

document breaking change around NoTrackingWithIdentityResolution (disabled scenario)

3 participants